/***************************************************************************
 * 
 * Copyright (c) 2011 Baidu.com, Inc. All Rights Reserved
 * 
 **************************************************************************/
 
 
 
/**
 * @file strategy_engine_manager.cpp
 * @author liutao08(com@baidu.com)
 * @date 2011/12/18 12:39:22
 * @brief 
 *  
 **/

#include "strategy_engine_manager.h"

bool StrategyEngineManager::LoadEngines(const char* stConf, CommunicationManager* cManager)
{
    xmlKeepBlanksDefault(0);
    xmlDocPtr docPtr = xmlReadFile(stConf, "UTF-8", XML_PARSE_RECOVER);
    if (NULL == docPtr)
    {
        FATAL("Document not parsed successfully: [%s]", stConf);
        return false;
    }
    xmlNodePtr curNode = xmlDocGetRootElement(docPtr);
    if (NULL == curNode)
    {
        FATAL("empty document : [%s]", stConf);
        return false;
    }
    if (xmlStrcmp(curNode->name,  BAD_CAST "strategyEngines")) 
    {
        FATAL("document of the wrong type, root node != strategyEngines");
        return false;
    }
    std::string engineName = ""; 
    for(curNode = curNode->xmlChildrenNode; curNode != NULL; curNode = curNode->next)
    {        
        if (xmlStrcmp(curNode->name,  BAD_CAST "strategies"))
        {
            WARNING("document of the wrong type, child node:[%s] != strategies", (char*)curNode->name);
            continue;
        }
        xmlChar* seAttr = xmlGetProp(curNode, BAD_CAST "name");
        engineName = reinterpret_cast<char *>(seAttr);
        xmlFree(seAttr);
        std::map<std::string, StrategyEngine*>::iterator iter = m_StrategyEngineTable.find(engineName);
        if(iter == m_StrategyEngineTable.end())
        {
            StrategyEngine* stEngine = new(std::nothrow) StrategyEngine();
            if (NULL == stEngine)
            {
                FATAL("Malloc engine [%s] is fail!", engineName.c_str());
                return false;
            }
            if (!stEngine->Init(cManager, curNode))
            {
                FATAL("Init engine [%s] is fail!", engineName.c_str());
                return false;
            }
            m_StrategyEngineTable[engineName] = stEngine;
        }
        else
        {
            FATAL("Engine [%s] is exist!", engineName.c_str());
            return false;
        }
    }
    return true;
}
void StrategyEngineManager::ResetStrategys(CommunicationManager* cManager)
{
    std::map<std::string, StrategyEngine*>::iterator iter = m_StrategyEngineTable.begin();
    while (iter != m_StrategyEngineTable.end())
    {
        iter->second->ResetStrategys(cManager);
        ++iter;
    }
}

StrategyEngine* StrategyEngineManager::GetStrategyEngine(const std::string& engineName)
{
    std::map<std::string, StrategyEngine*>::iterator iter = m_StrategyEngineTable.find(engineName);
    if (iter != m_StrategyEngineTable.end())
    {
        return iter->second;
    }
    else
        return NULL;
}

















/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */
